package littlecyberwar.tool;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;

import littlecyberwar.ui.*;
import littlecyberwar.util.*;

public class MouseRotateCenterHandler extends MouseRotateHandler {

// a common superclass for different kind of mouse action to game command
// i.e. move straight ahead
// rotate
// side step, etc.

//  Point2D pivotP;
//  double startDragX, startDragY, pivotX, pivotY;
 // double halfWidth, halfHeight;

  public MouseRotateCenterHandler() {
    super();
    id = H_ROTATECENTER;
	unitSelectedText = "Drag mouse around the unit centerpoint and release button to rotate unit.";
	noUnitSelectedText = "Click on unit to select for rotation around the center.";

  }

	public void startAction( MouseEvent e ) {
	   super.startAction(e);
	}
  
  void setPivotParam() {
	for ( int i=0; i < selectedElements.size(); i++ ){
		SelectedElementContainer cont = ((SelectedElementContainer)selectedElements.get(i));
		cont.halfWidth = cont.element.getWidth() / 2;
		cont.halfHeight = cont.element.getHeight() / 2;
	}

	currEl = ((SelectedElementContainer)selectedElements.get(0)).element;
	Shape selRec = currEl.getFinalShape();
	pivotX = selRec.getBounds2D().getCenterX();
	pivotY = selRec.getBounds2D().getCenterY();
	pivotP = new Point2D.Double(pivotX, pivotY);
	return;
  }

	// must be called after getCurrentDiameter()
  void setGroupPivotParam() {	
	for ( int i=0; i < selectedElements.size(); i++ ){
		SelectedElementContainer cont = ((SelectedElementContainer)selectedElements.get(i));
		
		/**
		 * For the halfHeight and halfWidth, we have to take the pivotX,
		 * inverse transform it back to the coordinate of the shape 
		 */
		Element el = cont.element;
		AffineTransform elTrans = el.getTransform();
		Point2D invTransedPivotP = null;
		
		try {
			invTransedPivotP = elTrans.inverseTransform(pivotP, invTransedPivotP );
						
		} catch ( Exception ex ) {
			ex.printStackTrace();
		}

		cont.halfWidth = invTransedPivotP.getX();
		cont.halfHeight = invTransedPivotP.getY();
	}

	// in case of group select, the pivotX, pivotY, and pivotP are all set
	return;
  }

  double getCurrentDiameter(boolean isCombinedShape){
	  double largestWidth= 0;
	  if ( isCombinedShape == false ){
		  // need to find the largest element
		  for ( int i=0; i < selectedElements.size(); i++ ) {
			  Element el = ((SelectedElementContainer)selectedElements.get(i)).element;
			  double currWidth =el.getWidth(); 
			  if ( currWidth > largestWidth ){
				  largestWidth = currWidth;
			  }
		  }
			
	  }else{
		  // if combined shape, then need to figure out the combined shape
		  // then take the distance from the center of the combined shape,
		  // the largestWidth is then the larger of the hwight/width of the
		  // bounds rectangle 2D.
		  //
		Area groupArea = new Area();
		for ( int i=0; i < selectedElements.size(); i++ ) {
			Element el = ((SelectedElementContainer)selectedElements.get(i)).element;
			Area a = new Area(el.getFinalShape());
			groupArea.add(a);
			Rectangle2D groupRec = groupArea.getBounds2D();
			
			// setup the pivot point here
			pivotX = groupRec.getCenterX();
			pivotY = groupRec.getCenterY();
			pivotP = new Point2D.Double(pivotX, pivotY);
			
			double groupWidth = groupRec.getWidth();
			double groupHeight = groupRec.getHeight();
			if ( groupWidth > groupHeight ){
				largestWidth = groupWidth;
			} else {
				largestWidth = groupHeight;
			}
		}		
	  }
	  return(largestWidth);
  }

}
